---初始化以及常量
local TASK<const> = require("task")
local ListTasks = {} ---@type Tasks[]

local MONSTER<const> = require("monster")
local ListMonsters = {} ---@type table[]


---@export
---@desc 创建任务集
---@param binder Role
---@return integer
function CreateTasks(binder)
    local _tasks = TASK.Tasks.new(binder)
    table.insert(ListTasks, _tasks)
    binder["burger_task"] = _tasks
    _tasks.order = #ListTasks
    return _tasks.order
end

---@export
---@desc 初始化任务集
---@param tasks integer
---@param source string
function ImportTasks(tasks, source)
    local _tasks = ListTasks[tasks]
    local path = "task@" .. source
    local ok, content = pcall(require, path)
    if ok then
        _tasks:import(content)
    else
        GlobalAPI.error(string.format("任务表<%s>不存在", source))
    end
end

---@export
---@desc 通过编号获取任务
---@param tasks integer 任务集
---@param order integer 任务序号
---@return integer
function GetTask(tasks, order)
    local _tasks = ListTasks[tasks]
    if order > 0 and order <= #_tasks.list then
        return _tasks.order * 10000 + order
    end
        GlobalAPI.error(string.format("任务集中不存在编号为 [%s] 的任务", tostring(order)))
    return 0
end

---@export
---@desc 增加任务条件计数
---@param task integer
---@param order integer
---@param score integer
function AddScoreToTask(task, order, score)
    local _tasks = ListTasks[math.tointeger(task // 10000)] ---@type Tasks
    local _task = _tasks.list[math.tointeger(task % 10000)] ---@type Task
    if order == -1 then
        for i = 1, #_task.cur_count do
            _task:add_score(i, score)
        end
    else
        _task:add_score(order, score)
    end
end

---@export
---@desc 领取任务奖励
---@param task integer
function GetRewardOfTask(task)
    local _tasks = ListTasks[math.tointeger(task // 10000)] ---@type Tasks
    local _task = _tasks.list[math.tointeger(task % 10000)] ---@type Task
    _task:get_reward()
end

---@export
---@desc 获取任务集属性
---@param tasks integer
---@param trigger Trigger
function GetAttributesOfTasks(tasks, trigger)
    local _tasks = ListTasks[tasks]
    local _task_list = {} ---@type integer[]
    for order = 1, #_tasks.list do
        table.insert(_task_list, _tasks.list[order].order)
    end
    trigger.set_kv_by_type(Enums.ValueType.Str, "名称", _tasks.name)
    trigger.set_kv_by_type(Enums.ValueType.ListInt, "任务列表", _task_list)
    trigger.set_kv_by_type(Enums.ValueType.Role, "所属角色", _tasks.ctrl_role)
end

---@export
---@desc 获取任务属性
---@param task integer
---@param trigger Trigger
function GetAttributesOfTask(task, trigger)
    local _tasks = ListTasks[math.tointeger(task // 10000)]
    local _task = _tasks.list[math.tointeger(task % 10000)]
    local _cur_count = 0
    local _tar_count = 0
    for i, value in ipairs(_task.cur_count) do
        _cur_count = _cur_count + value
        _tar_count = _tar_count + _task.tar_count[i]
    end
    trigger.set_kv_by_type(Enums.ValueType.Str, "名称", _task.name)
    trigger.set_kv_by_type(Enums.ValueType.Str, "描述", _task.desc)
    trigger.set_kv_by_type(Enums.ValueType.Int, "权重", math.tointeger(_task.weight))
    trigger.set_kv_by_type(Enums.ValueType.Str, "类型", tostring(_task.kind))
    trigger.set_kv_by_type(Enums.ValueType.Int, "当前进度", math.tointeger(_cur_count))
    trigger.set_kv_by_type(Enums.ValueType.Int, "目标进度", math.tointeger(_tar_count))
    trigger.set_kv_by_type(Enums.ValueType.Int, "所属任务集", math.tointeger(_tasks.order))
end

---@export
---@desc 绑定任务集界面
---@param tasks Tasks
---@param units ListENode
---@param names ListENode
---@param descs ListENode
---@param counts ListENode
---@param tag ENode
---@param page ENode
function BindTasksPage(tasks, units, names, descs, counts, tag, page)
    local _tasks = ListTasks[tasks]
    local _role = _tasks.ctrl_role
    _tasks.units = units
    _tasks.names = names
    _tasks.descs = descs
    _tasks.counts = counts
    _tasks.tag = tag
    _tasks.page_tag = page
    _role.set_label_text(tag, _tasks.name)
    _tasks:display(1)
end

---@export
---@desc 更改任务页码
---@param tasks Tasks
---@param page integer
function TurnTasksPage(tasks, page)
    local _tasks = ListTasks[tasks]
    local _role = _tasks.ctrl_role
    if page == 0 then
        page = _tasks.page + 1
    elseif page == -1 then
        page = _tasks.page - 1
    end
    if page < 1 then
        page = 1
    elseif page > #_tasks.list // #_tasks.names + 1 then
        page = #_tasks.list // #_tasks.names + 1
    end
    _tasks:display(page)
    if _role.has_kv("本次标签页") then
        _role.set_attr_by_type(Enums.ValueType.ENode, "上次标签页", _role.get_attr_by_type(Enums.ValueType.ENode, "本次标签页"))
        _role.set_attr_by_type(Enums.ValueType.ENode, "本次标签页", _tasks.tag)
    else
        _role.set_attr_by_type(Enums.ValueType.ENode, "上次标签页", _tasks.tag)
        _role.set_attr_by_type(Enums.ValueType.ENode, "本次标签页", _tasks.tag)
    end
end

---@export
---@desc 导入怪物集
---@param source string
function ImportMonsters(source)
    MONSTER.Import(source)
end